<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/21
 * Time: 18:11
 */
namespace app\business\controller;

use think\Controller;
use think\captcha\Captcha;
use think\Db;

class Forget extends Controller
{
	/**
	 * 忘记密码首页
	*/
	public function index()
	{
		return view('index');
	}
	
	/**
	 * 验证码
	*/
	public function code()
	{
		$captcha = new Captcha();
		$captcha->fontSize = 30;
		$captcha->length = 4;
		$captcha->expire = 300;
		return $captcha->entry();
	}
	
	/**
	 * 发送修改密码邮件
	 * @param string post.email 邮箱
	 * @param string post.code 验证码
	*/
	public function sendemail()
	{
		if(request()->isAjax()){
			try{
				$code = input('post.code');
				if(!captcha_check($code)){
					throw new \PDOException('验证码错误');
				}
				$email = input('post.email');
				$result = Db::table('business')->where('email',$email)->where('status',2)->find();
				if($result){
					//生成token
					$token = $this->token();
					$info = Db::table('business')->where('email',$email)->where('status',2)->update(['token'=>$token]);
					if(!$info){
						throw new \PDOException('账号不存在');
					}
					$title = "新品有戏商家合作平台注册";
					$content = "亲爱的" . $email . "：<br/>请点击链接修改您的密码。<br/><a href='http://localhost/company/shoping/shop/public/business/forget/updatepass?token=" .$token. "' target='_blank'>http://localhost/company/shoping/shop/public/business/forget/updatepass?token=" .$token."</a><br/>如果以上链接无法点击，请将它复制到你的浏览器地址栏中进入访问，该链接24小时内有效。<br/>如果此次修改请求非你本人所发，请忽略本邮件。<br/>";
					$result = sendMail($email,$title,$content);
					if($result){
						//发送成功
						$success = ['success'=>"邮件发送成功，请注意查看邮箱"];
						return json($success);
					}
				}else{
					throw new \PDOException('邮箱不存在');
				}
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 修改密码页面
	 * @param string get.token 标识
	*/
	
	public function updatepass()
	{
		if(trim(input('get.token'))){
			$token = input('get.token');
			return view('updatepass',compact('token'));
		}else{
			return view('fail');
		}
	}
	
	/**
	 * 修改密码
	 * @param string post.token 标识
	 * @param string post.pass 确认密码
	 * @param string post.pass 密码
	*/
	public function updatepassword()
	{
		if(request()->isAjax()){
			try{
				$token = trim(input('post.token'));
				$password = trim(input('post.password'));
				$pass = trim(input('post.pass'));
				if(!$token){
					throw new \PDOException('已失效，请重新获取');
				}
				if(!$password){
					throw new \PDOException('请输入密码');
				}
				if(!$pass){
					throw new \PDOException('请输入密码');
				}
				$pass = encrypt($pass);
				$result = Db::table('business')->where('token',$token)->update(['password'=>$pass,'token'=>'']);
				if(!$result){
					throw new \PDOException('修改失败');
				}
				$success = ['success'=>'修改成功'];
				return json($success);
			}catch (\PDOException $e){
				$error = ['error'=>$e->getMessage()];
				return json($error);
			}
		}
	}
	
	/**
	 * 生成token
	 * @return string $token
	 * */
	public function token($length = 10)
	{
		// 密码字符集，可任意添加你需要的字符
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$token = time();
		for ( $i = 0; $i < $length; $i++ )
		{
			$token .= $chars[ mt_rand(0, strlen($chars) - 1) ];
		}
		return $token;
	}
}